package com.example.leetcode.math;

/**
 * 你总共有 n 枚硬币，并计划将它们按阶梯状排列。对于一个由 k 行组成的阶梯，其第 i 行必须正好有 i 枚硬币。阶梯的最后一行 可能 是不完整的。
 *
 * 给你一个数字 n ，计算并返回可形成 完整阶梯行 的总行数。
 */
public class leetcode223 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2;
        System.out.println(solution.computeArea(ax1, ay1, ax2, ay2, bx1, bx2, by1, by2));
    }

    static class Solution {
        public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
            int areaA = (ax2 - ax1) * (ay2 - ay1);
            int areaB = (bx2 - bx1) * (by2 - by1);

            int overlapWidth = Math.max(0, Math.min(ax2, bx2) - Math.max(ax1, bx1));
            int overlapHeight = Math.max(0, Math.min(ay2, by2) - Math.max(ay1, by1));
            int overlapArea = overlapWidth * overlapHeight;

            return areaA + areaB - overlapArea;
        }
    }

}
